<template>
  <a-modal
    :title="title"
    :width="720"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="工种名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input
            placeholder="请输入工种名称"
            v-decorator="['subject_name', {rules: [{required: true, message: '请输入工种名称'}]}]"
          />
        </a-form-item>
        <a-form-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-radio-group
            v-decorator="['status', {initialValue:0}]"
          >
            <a-radio :value="0">
              启用
            </a-radio>
            <a-radio :value="1">
              停用
            </a-radio>
          </a-radio-group>
        </a-form-item>

        <!--        <a-divider orientation="left">-->
        <!--          工种等级-->
        <!--        </a-divider>-->

        <!--        <table class="table" border="1">-->
        <!--          <tr style="font-weight: bold">-->
        <!--            <td>等级</td>-->
        <!--            <td>绑定题库</td>-->
        <!--            <td>操作</td>-->
        <!--          </tr>-->
        <!--          <tr v-for="item in level_list">-->
        <!--            <td>{{ item.level_name }}</td>-->
        <!--            <td>-->
        <!--              <a-select-->
        <!--                v-model="item.question_id"-->
        <!--              >-->
        <!--                <a-select-option value="jack">-->
        <!--                  Jack-->
        <!--                </a-select-option>-->
        <!--                <a-select-option value="lucy">-->
        <!--                  Lucy-->
        <!--                </a-select-option>-->
        <!--                <a-select-option value="disabled" disabled>-->
        <!--                  Disabled-->
        <!--                </a-select-option>-->
        <!--                <a-select-option value="Yiminghe">-->
        <!--                  yiminghe-->
        <!--                </a-select-option>-->
        <!--              </a-select>-->
        <!--            </td>-->
        <!--            <td><a style="color: red">删除</a></td>-->
        <!--          </tr>-->
        <!--        </table>-->
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

import * as Api from '@/api/subject/index'
import { STable, UploadImage } from '@/components'

export default {
  components: {
    STable,
    UploadImage
  },
  data () {
    return {
      // 对话框标题
      title: '',
      // 标签布局属性
      labelCol: {
        span: 7
      },
      // 输入框布局属性
      wrapperCol: {
        span: 13
      },
      // modal(对话框)是否可见
      visible: false,
      // modal(对话框)确定按钮 loading
      confirmLoading: false,
      // 当前表单元素
      form: this.$form.createForm(this),
      // 当前记录
      record: {},
      // 当前模式
      mode: '',
      level_list: [
        { level_name: '初级', question_id: '' },
        { level_name: '中级', question_id: '' },
        { level_name: '高级', question_id: '' },
        { level_name: '--', question_id: '' }
      ]
    }
  },
  methods: {
    add () {
      this.mode = 'add'
      this.title = '新增工种'
      this.visible = true
    },

    edit (record) {
      this.mode = 'edit'
      this.title = '编辑工种'
      this.visible = true
      this.record = record
      // 设置默认值
      this.setFieldsValue()
    },

    /**
     * 设置默认值
     */
    setFieldsValue () {
      const { form: { setFieldsValue } } = this
      this.$nextTick(() => {
        const data = _.pick(this.record, ['subject_name', 'status'])
        setFieldsValue(data)
      })
    },

    /**
     * 确认按钮
     */
    handleSubmit (e) {
      e.preventDefault()
      const { form: { validateFields } } = this
      // 表单验证
      validateFields((errors, values) => {
        // 提交到后端api
        !errors && this.onFormSubmit(values)
      })
    },

    /**
     * 关闭对话框事件
     */
    handleCancel () {
      this.visible = false
      this.form.resetFields()
    },

    /**
     * 提交到后端api
     */
    onFormSubmit (values) {
      this.confirmLoading = true

      var api
      if (this.mode === 'add') {
        api = Api.add({ form: values })
      } else {
        api = Api.edit({ apiId: this.record['subject_id'], form: values })
      }
      api.then((result) => {
        // 显示成功
        this.$message.success(result.message, 1.5)
        // 关闭对话框
        this.handleCancel()
        // 通知父端组件提交完成了
        this.$emit('handleSubmit', values)
      })
        .finally((result) => {
          this.confirmLoading = false
        })
    }

  }
}
</script>
<style scoped lang="less">
.table {
  width: 100%;
  border-color: #e8e8e8;
  text-align: center;

  td {
    width: 33.333%;
    padding: 5px 10px;
  }
}
</style>
